﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.Domain.Entity;
using TR.Domain.Repository.Sys_EncodeRepository;

namespace TR.EFCore.Repository.Sys_EncodeRepository
{
    public class Sys_EncodeRepository : ISys_EncodeRepository
    {
        private readonly MyContext myContext;

        public Sys_EncodeRepository(MyContext myContext)
        {
            this.myContext = myContext;
        }

        private int fyzts;//分页总条数

        //编码ID查询
        public async Task<sys_Encode> IDSelectAsync(int id)
        {
            var jg = await myContext.Senc.Where(u => u.en_code == id).FirstOrDefaultAsync();
            return jg;
        }

        //分页查询
        public async Task<List<sys_Encode>> SencFenyeSelectAsync(int mys, int dqy, string qsj, string hsj, sys_Encode sen)
        {
            //变量声明 Linq 语法使用
            var query = myContext.Senc.AsQueryable();
            query = query.Where(p => p.is_del == 0);

            if (!string.IsNullOrEmpty(qsj) && !string.IsNullOrEmpty(hsj))
            {
                query = query.Where(p => string.Compare(p.create_time, qsj) >= 0
                               && string.Compare(p.create_time, hsj) <= 0);
            }

            ////条件  StringComparison.OrdinalIgnoreCase 模糊查询
            if (sen != null)
            {
                if (!string.IsNullOrEmpty(sen.en_form))
                {
                    query = query.Where(p => EF.Functions.Like(p.en_form, "%" + sen.en_form + "%"));
                }
                if (!string.IsNullOrEmpty(sen.en_prefix))
                {
                    query = query.Where(p => EF.Functions.Like(p.en_prefix, "%" + sen.en_prefix + "%"));
                }
                if (!string.IsNullOrEmpty(sen.en_time))
                {
                    query = query.Where(p => p.en_time == sen.en_time);
                }

            }
            var result = await query
                 .OrderByDescending(p => p.last_date)
                  .Skip(mys * (dqy - 1))
                  .Take(mys)
                  .ToListAsync();

            fyzts = await query.CountAsync(); // 获取符合查询条件的记录数

            return result; // 返回查询结果
        }

        //总条数
        public async Task<int> SencZTSelectAsync()
        {
            return fyzts;
        }

        //条件查询单条
        public async Task<sys_Encode> TjSelectAsync(string tj)
        {
            return await myContext.Senc.FirstOrDefaultAsync(e => e.en_form == tj);
        }

        //修改
        public async Task<bool> UpdelectAsync(sys_Encode sen)
        {
            myContext.Senc.Update(sen);
            return await myContext.SaveChangesAsync() > 0 ? true : false;
        }

        //编码规则新增
        public async Task<bool> InsertAsync(sys_Encode senadd)
        {
            myContext.Senc.Add(senadd);
            return await myContext.SaveChangesAsync() > 0 ? true : false;
        }
    }
}
